package com.bi.bitest.bao.fragments.discover;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ListView;
import com.bi.bitest.bao.Album_Detail_Activity;
import com.bi.bitest.bao.Constants;
import com.bi.bitest.bao.R;
import com.bi.bitest.bao.adapters.DiscoverRecommendAdapter;
import com.bi.bitest.bao.entity.discoverrecommend.*;
import com.bi.bitest.bao.fragments.BaseFragment;
import com.bi.bitest.bao.tasks.DiscoverRecommendTask;
import com.bi.bitest.bao.tasks.TaskCallback;
import com.bi.bitest.bao.tasks.TaskResult;
import com.bi.bitest.bao.widgets.TopView_discover_recommend;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverRecommendFragment extends BaseFragment implements TaskCallback, View.OnClickListener {

    private DiscoverRecommendAdapter adapter;
    private List<DiscoverRecommendItem> items;
    private TopView_discover_recommend topView;
    private List<String>headerList;

    public DiscoverRecommendFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        items = new ArrayList<DiscoverRecommendItem>();
        adapter = new DiscoverRecommendAdapter(getActivity(), items);
        topView=new TopView_discover_recommend(getActivity());

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover_recommend, container, false);

        ListView listView = (ListView) view.findViewById(R.id.discover_recommend_list);
        //加Header的数据
        headerList=new ArrayList<String>();

        listView.addHeaderView(topView);

        //设置点击专辑事件
        adapter.setOnrRecommendAlbumClickListener(this);
        listView.setAdapter(adapter);

        return view;
    }

    /**
     * 因为每次进入 推荐的时候，都会触发内容的重写加载，所以在onResume中进行内容的加载
     */
    @Override
    public void onResume() {
        super.onResume();

        DiscoverRecommendTask task = new DiscoverRecommendTask(this);
        task.execute();

    }

    @Override
    public String getFragmentTitle() {
        return "推荐";
    }

    @Override
    public void onTaskFinished(TaskResult taskResult) {
        // TODO 处理推荐列表数据
        if(taskResult !=  null){

            if(taskResult.action == Constants.TASK_ACTION_DISCOVER_RECOMMENDS){
                if(taskResult.retCode == Constants.TASK_RESULT_OK){
                    Object data = taskResult.data;
                    if(data != null) {
                        if(data instanceof List){
                            List list = (List) data;
                            // TODO 更新 ListView Adapter

                            //只要数据来了，就清除 Adapter
                            items.clear();
                            //如果headerList有数据就不执行此步骤 防止多次加载重复数据
                            for (Object o : list) {
                                //如果是header则获取图片地址
                                if(o instanceof DiscoverRecommendHeader){
                                    if(headerList.size()==0) {
                                        DiscoverRecommendHeader header = (DiscoverRecommendHeader) o;
                                        List<HeaderItem> items = header.getHeaderItems();
                                        List<String> headl = new ArrayList<String>();
                                        for (HeaderItem item : items) {
                                            headl.add(item.getPic());
                                        }
                                        headerList.addAll(headl);
                                        topView.setData(headerList);
                                    }
                                }
                                //否则继续先前加载
                                    else{
                                    if (o instanceof DiscoverRecommendItem) {
                                        items.add((DiscoverRecommendItem) o);
                                    }
                                }
                            }
                            adapter.notifyDataSetChanged();
                        }
                    }


                }else{
                    // TODO 提示错误信息
                }


            }

        }


    }

    @Override
    public void onClick(View v) {
        Object tag=v.getTag();
        if(tag!=null){
           if(tag instanceof AlbumRecommend){
                AlbumRecommend albumRecommend=(AlbumRecommend)tag;
               long albumId= albumRecommend.getAlbumId();//专辑ID
                long trackId=albumRecommend.getTrackId();//曲目ID
               Intent intent=new Intent(getActivity(), Album_Detail_Activity.class);
                 intent.putExtra("albumid",albumId);

               intent.putExtra("trackid",trackId);
               startActivity(intent);
            }



        }
    }
}
